<template>
  <div class="app-container">
    <div
        class="header"
        style="display: flex; justify-content: space-between"
    >
      <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
      >
        <el-form-item>
          <el-button
              icon="refresh"
              color=" rgb(69, 76, 105)"
              style="color: #fff"
              @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              @click="handleExport"
              color="rgb(146,226,206)"
              style="color: #fff; padding: 0 20px; text-shadow: 0 0 4px #32003c"
              v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
      </el-form>
      <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
      >
        <el-form-item
            label="ID"
            prop="passengerId"
        >
          <el-input
              v-model="queryParams.passengerId"
              placeholder="请输入乘客ID"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
            label="姓名"
            prop="userName"
        >
          <el-input
              v-model="queryParams.userName"
              placeholder="请输入乘客姓名"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
            label="手机号"
            prop="phone"
        >
          <el-input
              v-model="queryParams.phone"
              placeholder="请输入乘客手机号"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="注册时间" style="width: 308px;">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>


        <el-form-item>
          <el-button
              type="info"
              icon="Search"
              @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>
    <el-table
        v-loading="loading"
        :data="passengerList"
        @selection-change="handleSelectionChange"
    >
      <!--      <el-table-column-->
      <!--        type="selection"-->
      <!--        width="55"-->
      <!--        align="center"-->
      <!--      />-->
      <el-table-column label="头像" align="center" prop="avatar">
        <template #default="scope">
          <el-image style="width: 100px; height: 80px; display: block" v-if="scope.row.avatar"
                    :src="scope.row.avatar"
                    it="contain"
                    title="点击查看大图"
                    :preview-src-list="[scope.row.avatar]"
                    preview-teleported
                    hide-on-click-modal>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
          label="ID"
          align="center"
          prop="passengerId"
      />
      <el-table-column
          label="乘客昵称"
          align="center"
          prop="nickName"
      />
      <el-table-column
          label="乘客姓名"
          align="center"
          prop="userName"
      />
      <el-table-column
          label="乘客手机号"
          align="center"
          prop="phone"
      />
      <el-table-column
          label="紧急联系人号码"
          align="center"
          prop="emergencyPhone"
      />
      <el-table-column
          label="余额"
          align="center"
          prop="balance"
      />
      <el-table-column label="是否合伙人" align="center" prop="isCopartner">
        <template #default="scope">
          <el-tag v-if="scope.row.isCopartner == 0" type="primary">否</el-tag>
          <el-tag v-if="scope.row.isCopartner == 1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          label="贡献分"
          align="center"
          prop="contributeScore"
      />
      <el-table-column
          label="邀请人ID"
          align="center"
          prop="accountStatus"
      />
      <el-table-column
          label="累计邀请下单"
          align="center"
          prop="accountStatus"
      />
      <el-table-column label="账号状态" align="center" prop="accountStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.accountStatus == 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.accountStatus == 1" type="primary">注销</el-tag>
          <el-tag v-if="scope.row.accountStatus == 2" type="danger">封停</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          label="注册时间"
          align="center"
          prop="registerTime"
          width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.registerTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-popover trigger="click" placement="top" :width="160" :visible="scope.row.visiblePopover"
                      @show="scope.row.visiblePopover = true" @hide="scope.row.visiblePopover = false">
            <div>
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:passenger:edit']"
              >修改详情
              </el-button>
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleBalance(scope.row)"
                  v-hasPermi="['system:passenger:edit']"
              >修改余额
              </el-button>
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="accountBan(scope.row)"
                  v-hasPermi="['system:passenger:edit']"
              >账号封停
              </el-button>
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="resetPassenger(scope.row)"
                  v-hasPermi="['system:passenger:edit']"
              >重置密码
              </el-button>
            </div>
            <template #reference>
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="scope.row.visiblePopover = true"
                  v-hasPermi="['system:passenger:edit']"
              >修改
              </el-button>
            </template>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!--  重置密码弹窗  -->
    <el-dialog
        :title="title"
        v-model="open3"
        width="500px"
        append-to-body
    >
      <div style="text-align: center; font-size: 15px">
        是否要重置该账号密码?
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button
              type="primary"
              @click="submitForm3"
          >确 定
          </el-button
          >
          <el-button @click="cancel3">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 账号封停弹出框 -->
    <el-dialog
        :title="title"
        v-model="open2"
        width="500px"
        append-to-body
    >
      <el-form
          ref="passengerRef"
          :model="form2"
          :rules="rules"
          label-width="80px"
      >
        <el-form-item
            label="昵称"
            prop="nickName"
        >
          <el-input
              v-model="form.nickName"
              placeholder="请输入昵称"
          />
        </el-form-item>
        <el-form-item
            label="手机号"
            prop="phone"
        >
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
              type="primary"
              @click="submitForm2"
          >确 定
          </el-button
          >
          <el-button @click="cancel2">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!--  修改余额弹出框  -->
    <el-dialog
        :title="title"
        v-model="open1"
        width="500px"
        append-to-body
    >
      <el-form
          ref="passengerRef"
          :model="form1"
          :rules="rules"
          label-width="80px"
      >
        <el-form-item
            label="昵称"
            prop="nickName"
        >
          <el-input
              v-model="form.nickName"
              placeholder="请输入昵称"
          />
        </el-form-item>
        <el-form-item
            label="手机号"
            prop="phone"
        >
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item
            label="余额"
            prop="balance"
        >
          <el-input
              v-model="form.balance"
              placeholder="请输入余额"
          />
        </el-form-item>
        <el-form-item
            label="类型"
            prop=""
        ></el-form-item>
        <el-form-item
            label="数量"
            prop=""
        ></el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
              type="primary"
              @click="submitForm1"
          >确 定
          </el-button
          >
          <el-button @click="cancel1">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改乘客管理对话框 -->
    <el-dialog
        :title="title"
        v-model="open"
        width="500px"
        append-to-body
    >
      <el-form ref="passengerRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="头像" prop="avatar">
          <el-image style="width: 100px; height: 80px; display: block" v-if="form.avatar"
                    :src="form.avatar" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.avatar]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>

        <el-form-item
            label="昵称"
            prop="nickName"
        >
          <el-input
              v-model="form.nickName"
              placeholder="请输入昵称"
          />
        </el-form-item>
        <el-form-item
            label="手机号"
            prop="phone"
        >
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
          />
        </el-form-item>

        <el-form-item label="是否合伙人" prop="form.isCopartner">
          <el-select v-model="form.isCopartner" placeholder="请选择类型">
            <el-option
                v-for="item in Typevalue"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
              type="primary"
              @click="submitForm"
          >确 定
          </el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Passenger">
import {
  listPassenger,
  getPassenger,
  delPassenger,
  addPassenger,
  updatePassenger,
  banPassenger,
  resetPassword,
} from "@/api/system/passenger";

const {proxy} = getCurrentInstance();

const passengerList = ref([]);
const open = ref(false);
const open1 = ref(false);
const open2 = ref(false);
const open3 = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const visiblePopover = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  form1: {},
  form2: {},
  form3: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    passengerId: null,
    userName: null,
    phone: null,
  },
  rules: {},
});

const {queryParams, form, rules, form1, form2, form3} = toRefs(data);

/** 查询乘客管理列表 */
function getList() {
  loading.value = true;
  listPassenger(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    passengerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const Typevalue = ref([
  {
    label: "否",
    value: 0
  },
  {
    label: "是",
    value: 1
  },

])

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

function cancel1() {
  open1.value = false;
  form1.value = {};
}

function cancel2() {
  open2.value = false;
  form2.value = {};
}

function cancel3() {
  open3.value = false;
  form3.value = {};
}

// 表单重置
function reset() {
  form.value = {
    passengerId: null,
    avatar: null,
    nickName: null,
    userName: null,
    phone: null,
    emergencyPhone: null,
    balance: null,
    isCopartner: null,
    contributeScore: null,
    accountStatus: null,
    registerTime: null,
    status: null,
    belongCity: null,
  };
  proxy.resetForm("passengerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.passengerId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加乘客管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // reset();
  // form.value = row;
  // 创建一个新对象并拷贝 row 的数据
  form.value = {...row};
  open.value = true;
  title.value = "修改详情";
}

function handleBalance(row) {
  form1.value = {...row};
  open1.value = true;
  title.value = "修改余额";
}

function accountBan(row) {
  form2.value = {...row};
  open2.value = true;
  title.value = "账号封停";
}

function resetPassenger(row) {
  form3.value = {...row}; // 复制行数据到 form3
  // console.log(row)
  open3.value = true; // 打开重置密码对话框
  title.value = "重置密码";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["passengerRef"].validate((valid) => {
    if (valid) {
      if (form.value.passengerId != null) {
        updatePassenger(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function submitForm1() {
}

function submitForm2() {
  banPassenger(form2.value).then((response) => {
    proxy.$modal.msgSuccess("封禁成功");
    open2.value = false;
    getList();
  });
}

function submitForm3() {
  resetPassword(form3.value).then((response) => {
    proxy.$modal.msgSuccess("重置成功");
    open3.value = false;
    getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _passengerIds = row.passengerId || ids.value;
  proxy.$modal
      .confirm('是否确认删除乘客管理编号为"' + _passengerIds + '"的数据项？')
      .then(function () {
        return delPassenger(_passengerIds);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      "system/passenger/export",
      {
        ...queryParams.value,
      },
      `passenger_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

